package com.innazis.jfrogsurvey.backendmanager;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.innazis.jfrogsurvey.config.Constants;
import com.innazis.jfrogsurvey.obj.AnswerObj;
import com.innazis.jfrogsurvey.obj.QuestionObj;
import com.innazis.jfrogsurvey.obj.SurveyObj;

public class ParserUtility {
	public static ArrayList<SurveyObj> parserListCategory(JSONArray arrayJSONSurveys) {
		
		ArrayList<SurveyObj> updateSurveysArray = new ArrayList<SurveyObj>();
		JSONObject obj = null, obj2 = null, obj3 = null;
		SurveyObj sObj;
		if (arrayJSONSurveys != null && arrayJSONSurveys.length() > 0) {
			try {
				for (int s = 0; s < arrayJSONSurveys.length(); s++) {
					sObj = new SurveyObj();
					obj = arrayJSONSurveys.getJSONObject(s);
					sObj.set_survey_id(getIntValue(obj, Constants.survey_id_p));
					sObj.set_survey_name(getStringValue(obj,
							Constants.survey_name_p));
					sObj.set_survey_title(getStringValue(obj,
							Constants.survey_title_p));
					sObj.set_questions_num(getIntValue(obj,
							Constants.questions_num_p));
					JSONArray arrayJSONQustions = new JSONArray(
							arrayJSONSurveys.getJSONObject(s).getString(
									Constants.questions_list_p));
					ArrayList<QuestionObj> updateQuestions = new ArrayList<QuestionObj>();
					QuestionObj qObj = null;
					for (int q = 0; q < arrayJSONQustions.length(); q++) {
						qObj = new QuestionObj();
						obj2 = arrayJSONQustions.getJSONObject(q);
						qObj.set_question_id(getIntValue(obj2,
								Constants.question_id_p));
						qObj.set_survey_id(getIntValue(obj2,
								Constants.survey_id_p));
						qObj.set_question_title(getStringValue(obj2,
								Constants.question_title_p));
						qObj.set_question_order(getIntValue(obj2,
								Constants.question_order_p));
						qObj.set_answers_num(getIntValue(obj2,
								Constants.answers_num_p));
						JSONArray arrayJSONAnswers = new JSONArray(
								arrayJSONQustions.getJSONObject(q).getString(
										Constants.answers_list_p));
						ArrayList<AnswerObj> updateAnswers = new ArrayList<AnswerObj>();
						for (int a = 0; a < arrayJSONAnswers.length(); a++) {
							AnswerObj aObj = new AnswerObj();
							obj3 = arrayJSONAnswers.getJSONObject(a);
							aObj.set_answer_id(getIntValue(obj3,
									Constants.answer_id_p));
							aObj.set_question_id(getIntValue(obj3,
									Constants.question_id_p));
							aObj.set_survey_id(getIntValue(obj3,
									Constants.survey_id_p));
							aObj.set_answer_order(getIntValue(obj3,
									Constants.answer_order_p));
							aObj.set_answer_text(getStringValue(obj3,
									Constants.answer_text_p));
							aObj.set_object_name(getStringValue(obj3,
									Constants.object_name_p));

							updateAnswers.add(aObj);
						}
						qObj.set_answers_list(updateAnswers);
						updateQuestions.add(qObj);
					}
					sObj.set_questions_list(updateQuestions);
					updateSurveysArray.add(sObj);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return updateSurveysArray;

	}

	// Core function. Please don't change it
	public static String getStringValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? "" : obj.getString(key);
		} catch (JSONException e) {
			return "";
		}
	}

	public static long getLongValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? 0L : obj.getLong(key);
		} catch (JSONException e) {
			return 0L;
		}
	}

	public static int getIntValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? 0 : obj.getInt(key);
		} catch (JSONException e) {
			return 0;
		}
	}

	public static Double getDoubleValue(JSONObject obj, String key) {
		double d = 0.0;
		try {
			return obj.isNull(key) ? d : obj.getDouble(key);
		} catch (JSONException e) {
			return d;
		}
	}

	public static boolean getBooleanValue(JSONObject obj, String key) {
		try {
			return obj.isNull(key) ? false : obj.getBoolean(key);
		} catch (JSONException e) {
			return false;
		}
	}
}
